<?php
include 'orgChart.php';
$org = new orgChart();
$elements = $org->tree;
$data = $org->buildOrgTree($elements);
if (isset($_GET['root']))
    $root = $_GET['root'];
function echoNode($element, $r = null) {
    if (!is_null($r)) {
        if ($element['id'] == $r) {
            if (isset($element['children'])) {
                echo "<li><a href='?root=" . $element['id'] . "'>" . $element['name'] . "</a>";
                echo "<p><span>" . $element['title'] . "</span><br><span>" . $element['department'] . "</span></p>";
                echo "<ul>";
                foreach ($element['children'] as $subItem) {
                    echoNode($subItem);
                }
                echo "</ul>";
                echo "</li>";
            } else {
                echo "<li>" . $element['name'];
                echo "<p><span>" . $element['title'] . "</span><br><span>" . $element['department'] . "</span></p>";
                echo "</li>";
            }
        } else {
            if (isset($element['children'])) {
                foreach ($element['children'] as $subItem) {
                    echoNode($subItem, $r);
                }
            }
        }
    } else {
        if (isset($element['children'])) {
            if (!is_null($r)) {
                
            }
            echo "<li><a href='?root=" . $element['id'] . "'>" . $element['name'] . "</a>";
            echo "<p><span>" . $element['title'] . "</span><br><span>" . $element['department'] . "</span></p>";
            echo "<ul>";
            foreach ($element['children'] as $subItem) {
                echoNode($subItem);
            }
            echo "</ul>";
            echo "</li>";
        } else {
            echo "<li>" . $element['name'];
            echo "<p><span>" . $element['title'] . "</span><br><span>" . $element['department'] . "</span></p>";
            echo "</li>";
        }
    }
}
?>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>S3 Corporation Organization Chart</title>
        <link rel="stylesheet" href="css/bootstrap.min.css"/>
        <link rel="stylesheet" href="css/jquery.jOrgChart.css"/>
        <link rel="stylesheet" href="css/custom.css"/>
        <link href="css/prettify.css" type="text/css" rel="stylesheet" />

        <script type="text/javascript" src="prettify.js"></script>

        <!-- jQuery includes -->
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>

        <script src="jquery.jOrgChart.js"></script>

        <script>
            jQuery(document).ready(function() {
                $("#org").jOrgChart({
                    chartElement : '#chart',
                    dragAndDrop  : true
                });
            });
        </script>
    </head>

    <body onload="prettyPrint();">
        <?php if (isset($root)): ?>
            <a style="font-weight: bold;font-size:16px;padding:20px" href="<?php echo $_SERVER["HTTP_REFERER"] ?>">< Back</a>    
            <?php endif; ?>    
        <ul id="org"  style="display:none" >
            <?php if (isset($root)): ?>      
                <?php foreach ($data as $item): ?>                  
                    <?php echoNode($item, $root); ?>         
                <?php endforeach; ?>
            <?php else: ?>
                <?php foreach ($data as $item): ?> 
                    <?php echoNode($item); ?>
                <?php endforeach; ?>
<?php endif; ?>    
        </ul>  

        <div id="chart" class="orgChart"></div>

        <script>
            jQuery(document).ready(function() {
            
                /* Custom jQuery for the example */
                $("#show-list").click(function(e){
                    e.preventDefault();
                
                    $('#list-html').toggle('fast', function(){
                        if($(this).is(':visible')){
                            $('#show-list').text('Hide underlying list.');
                            $(".topbar").fadeTo('fast',0.9);
                        }else{
                            $('#show-list').text('Show underlying list.');
                            $(".topbar").fadeTo('fast',1);                  
                        }
                    });
                });
            
                $('#list-html').text($('#org').html());
            
                $("#org").bind("DOMSubtreeModified", function() {
                    $('#list-html').text('');
                
                    $('#list-html').text($('#org').html());
                
                    prettyPrint();                
                });
            });
        </script>

    </body>
</html>

